//
//  FMDBTool.m
//  滴滴快拍
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FMDBTool.h"
#import "FMDB.h"
//#import "FMDatabaseAdditions.h"

@interface FMDBTool ()
@property (nonatomic, strong)FMDatabase *fmdb;
@property (nonatomic, strong)FMDatabase *favoriteFmdb;
@property (nonatomic, strong)NSString *filePath;
@property (nonatomic, strong)NSString *favoriteFilePath;

@end

@implementation FMDBTool

+ (instancetype)shareFMDBTool {
    static FMDBTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[FMDBTool alloc] init];
        
    });
    return tool;

}


- (void)openFavoriteDB {
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    self.favoriteFilePath = [sandBox stringByAppendingPathComponent:@"like.sqlite"];
    NSLog(@"222filePath: %@",self.favoriteFilePath);
    
    self.favoriteFmdb = [[FMDatabase alloc] initWithPath:self.favoriteFilePath];
    BOOL result = [self.fmdb open];
    if (result) {
        NSLog(@"打开222数据库");
    }else {
        NSLog(@"打开222失败");
    }

}


- (void)openDB {
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    self.filePath = [sandBox stringByAppendingPathComponent:@"channel.sqlite"];
    NSLog(@"filePath: %@",self.filePath);
    
    self.fmdb = [[FMDatabase alloc] initWithPath:self.filePath];
    BOOL result = [self.fmdb open];
    if (result) {
        NSLog(@"打开数据库");
    }else {
        NSLog(@"打开失败");
    }

}




- (void)createTable {
    NSString *sqlStr = @"create table if not exists channel(name text primary key, icon text, url text)";
    [self exeSqlWith:sqlStr showLog:@"创建表"];


}

- (void)createFavoriteTable {
    NSString *sqlStr = @"create table if not exists like(icon text primary key, nick text, lcnt nsnumber, scid text, picbase text, ft text, picm text, t text)";
    [self exeSqlWith:sqlStr showLog:@"222创建表"];
    
}


// 创建表和增删改都会调用这个方法  封装起来
- (void)exeSqlWith:(NSString *)sql showLog:(NSString *)log {
    // 使用fmdb执行sql语句
    BOOL result = [self.fmdb executeUpdate:sql];
    if (result) {
        NSLog(@"%@",[NSString stringWithFormat:@"%@成功",log]);
    }else {
        NSLog(@"%@",[NSString stringWithFormat:@"%@失败",log]);
        
    }
    
}


- (void)insertChannel:(Find *)channel {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into channel(name,icon,url) values('%@','%@','%@')" , channel.name,channel.icon,channel.url];
    [self exeSqlWith:sqlStr showLog:@"添加作者"];

}


- (void)insertFavorite:(AGLChannel *)channel {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into like(icon,nick,lcnt,scid,picbase,ft, picm,t) values('%@','%@','%@','%@','%@','%@','%@','%@')" , channel.icon,channel.nick,channel.lcnt,channel.scid,channel.picbase,channel.ft,channel.picm,channel.t];
    [self exeSqlWith:sqlStr showLog:@"222添加作者"];
}

//nick text primary key, icon text, lcnt nsnumber, scid text, picbase text, ft text, picm text, t text
- (void)deleteFavorite:(AGLChannel *)channel {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from like where icon = '%@' ",channel.icon ];
    [self exeSqlWith:sqlStr showLog:@"222删除"];

}

- (void)deleteChannel:(Find *)channel {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from channel where name = '%@' ",channel.name ];
    [self exeSqlWith:sqlStr showLog:@"删除"];


}
 
- (NSMutableArray *)selectFind {
    NSMutableArray *array = [NSMutableArray array];
    NSString *sqlStr = @"select * from channel";
    //
    FMResultSet *set = [self.fmdb executeQuery:sqlStr];
    // 执行查询语句
    // FMResultSet 的作用就是遍历表里符合数据的每一行
    while ([set next]) {
        Find *find = [[Find alloc] init];
        find.name = [set stringForColumn:@"name"];
        find.icon = [set stringForColumn:@"icon"];
        find.url = [set stringForColumn:@"url"];
        [array addObject:find];
    }
    
    return array;


}


- (NSMutableArray *)selectFavorite {
    NSMutableArray *array = [NSMutableArray array];
    NSString *sqlStr = @"select * from like";
    //
    FMResultSet *set = [self.fmdb executeQuery:sqlStr];
    // 执行查询语句
    // FMResultSet 的作用就是遍历表里符合数据的每一行
    while ([set next]) {
        AGLChannel *channel = [[AGLChannel alloc] init];
        
        channel.icon = [set stringForColumn:@"icon"];
        channel.nick = [set stringForColumn:@"nick"];
        channel.lcnt = [set objectForColumnName:@"lcnt"];
        channel.scid = [set stringForColumn:@"scid"];
        channel.picbase = [set stringForColumn:@"picbase"];
        channel.ft = [set stringForColumn:@"ft"];
        channel.picm = [set stringForColumn:@"picm"];
        channel.t = [set stringForColumn:@"t"];
        [array addObject:channel];
    }
    
    return array;


}

//nick text primary key, icon text, lcnt text, scid text, picbase text, ft text

- (BOOL)columnExists:(NSString *)columnName inTableWithName:(NSString *)tableName {
     FMResultSet *set = [self.fmdb executeQuery:[NSString stringWithFormat:@"select * from %@ where name = '%@'",tableName, columnName]];
    while ([set next]) {
        NSString *name = [set stringForColumn:@"name"];
        if ([name isEqualToString:columnName]) {
            return YES;
        }else {
            return NO;
        }
    }
    return NO;
}

- (BOOL)columnFavoriteExists:(NSString *)columnName inTableWithName:(NSString *)tableName {
    FMResultSet *set = [self.fmdb executeQuery:[NSString stringWithFormat:@"select * from %@ where icon = '%@'",tableName, columnName]];
    while ([set next]) {
        NSString *icon = [set stringForColumn:@"icon"];
        if ([icon isEqualToString:columnName]) {
            return YES;
        }else {
            return NO;
        }
    }
    return NO;
}


@end
